<?php

namespace App\Repositories;
use App\Exceptions\GeneralException;
use Exception;
use App\Models\Department;

class DepartmentRepository
{
	/**
	 * 创建部门
	 *
	 * Date: 18/04/2018
	 * @author George
	 * @param array $attributes
	 * @return $this|\Illuminate\Database\Eloquent\Model
	 * @throws GeneralException
	 */
	public function store(array $attributes)
	{
		try {
			return Department::create($attributes);
		} catch (Exception $exception) {
			dd($exception->getMessage());
			throw new GeneralException('创建部门失败', 500);
		}
	}

	/**
	 * 根据公司ID获取部门信息
	 *
	 * Date: 18/04/2018
	 * @author George
	 * @param $company_id
	 * @return \Illuminate\Database\Eloquent\Builder
	 */
	public function getByCompany($company_id, $name = false)
	{
		$query = Department::query();
		$query->where('company_id', $company_id);
		if ($name) {
			$query->where('name','like', "%{$name}%");
		}
		return $query;
	}
}
